package com.mfangsoft.zhuangjialong.common.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.FileSizeException;
import com.mfangsoft.zhuangjialong.common.model.ImageHeightWidthException;
import com.mfangsoft.zhuangjialong.common.model.ImageModel;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.model.updoadImgException;
import com.mfangsoft.zhuangjialong.common.utils.DateUtils;
import com.mfangsoft.zhuangjialong.common.utils.HttpRequest;
import com.mfangsoft.zhuangjialong.common.utils.ImgUtil;
import com.mfangsoft.zhuangjialong.common.utils.PropUtis;
import com.mfangsoft.zhuangjialong.common.utils.RedisUtils;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;

import sun.misc.BASE64Decoder;

@Controller
@RequestMapping("/common")
public class FileUploadController {

	@RequestMapping(value = "/updoad", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> upload(@RequestParam("file") MultipartFile file) {

		Long time = System.currentTimeMillis();

		java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

		String exname = FilenameUtils.getExtension(file.getOriginalFilename());
		String fileName =  f.format(new Date());
		String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/" + fileName;
		System.out.println("path--------------------" + path);
	
		
		ResponseMessage<String> message = new ResponseMessage<>();

		try {
			
			String result = httpClientUploadFile(file, path,  time + "." + exname);
			if(result.equals("\"success\"")){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				String resultPath = PropUtis.getValue(PropUtis.FILE_URL) + "/" + fileName + "/" + time + "."
						+ exname;
				message.setResult(resultPath);
				System.out.println("resultPath--------------------" + resultPath);
			};
			
			
		} catch (Exception e) {

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}

		return message;
	}
	
	/**
	 * 上传商户的logo
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/uploadUserLogo", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> uploadUserLogo(@RequestParam("file") MultipartFile file) {

		ResponseMessage<String> message = new ResponseMessage<>();
		try {
			BufferedImage bufImage = ImageIO.read(file.getInputStream());
			if(bufImage.getWidth() != 300 || bufImage.getHeight() != 300){
				throw new ImageHeightWidthException("");
			}
			Long time = System.currentTimeMillis();
	
			java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
	
			String exname = FilenameUtils.getExtension(file.getOriginalFilename());
			String fileName =  f.format(new Date());
			String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/" +fileName;
			String result = httpClientUploadFile(file, path,  time + "." + exname);
			if(result.equals("\"success\"")){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				String resultPath = PropUtis.getValue(PropUtis.FILE_URL) + "/" + fileName + "/" + time + "."
						+ exname;
				message.setResult(resultPath);
				System.out.println("resultPath--------------------" + resultPath);
			};
		} catch (IOException e) {

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		} catch (ImageHeightWidthException e) {
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage("请上传宽为300px，高为300px的图片");
			e.printStackTrace();
		}

		return message;
	}
	
	/**
	 * 上传集团商户的logo
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/uploadGroupUserLogo", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Map<String,Object>> uploadGroupUserLogo(@RequestParam("file") MultipartFile file) {
		ResponseMessage<Map<String,Object>> message = new ResponseMessage<>();
		try {
			BufferedImage bufImage = ImageIO.read(file.getInputStream());
			if(bufImage.getWidth() != 430 || bufImage.getHeight() != 430){
				throw new updoadImgException("请上传宽为430px，高为430px的图片");
			}
			Long time = System.currentTimeMillis();
	
			java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
	
			String exname = FilenameUtils.getExtension(file.getOriginalFilename());
	
			String format = f.format(new Date());
			
			String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/" + format;
			System.out.println("path--------------------" + path);
			//处理logo图片将其裁剪,转发用
			int random = (int)(Math.random()*900)+100;
            String uploadFile = httpClientUploadFile(file,path,time + "." + exname);
            if("\"success\"".equals(uploadFile)) {
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				String resultPath = PropUtis.getValue(PropUtis.FILE_URL) + "/" + format + "/" + time + "."
						+ exname;
				Map<String,Object> m=new HashMap<>();
				m.put("resultPath", resultPath);
				String cutImage = cutImage(path+"/" + time + "." + exname,path+"/" + time +random+ "." + exname,430, 336);
				if("success".equals(cutImage)) {
					String transpondImagePath = PropUtis.getValue(PropUtis.FILE_URL) + "/" + format + "/" + time + random + "." + exname;
					m.put("transpondImagePath", transpondImagePath);
				}
				message.setResult(m);
				System.out.println("resultPath--------------------" + resultPath);
			}else {
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage(SysConstant.FAILURE_MSG);
			}
		} catch (IOException e) {

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		} catch (updoadImgException e) {
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return message;
	}
	
	/**
	 * 界面管理活动图片的上传
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/updoadInterfaceActivityImg", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Object> updoadInterfaceActivityImg(@RequestParam("file") MultipartFile file) {
		ResponseMessage<Object> message = new ResponseMessage<Object>();
		ImageModel imageModel = new ImageModel();
		//上传的图片全名字
		String oldFileName = file.getOriginalFilename();
		//上传的图片的后缀
		String extFileName = oldFileName.substring(oldFileName.lastIndexOf("."));
		//文件后缀判断 .jpg
		if(!extFileName.matches("^.*(jpg)$")){
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage("图片格式不正确,请上传jpg格式的图片");
		}else{
			//判断是否是木马文件
			try {
				if(file.getSize() > 200*1024){
					throw new FileSizeException("");
				}
				BufferedImage bufImage = ImageIO.read(file.getInputStream());
				if(bufImage.getWidth() != 750 || bufImage.getHeight() != 1334){
					throw new ImageHeightWidthException("");
				}
				imageModel.setWidth(bufImage.getWidth());
				imageModel.setHeight(bufImage.getHeight());
				String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/InterfaceImg/";
				System.out.println("path--------------------" + path);
				
				String fileName = UserContext.getCurrentUserId() + "001" +extFileName;
				String result = httpClientUploadFile(file, path,  fileName);
				if(result.equals("\"success\"")){
					message.setCode(SysConstant.SUCCESS_CODE);
					message.setMessage(SysConstant.SUCCESS_MSG);
					String resultPath = PropUtis.getValue(PropUtis.FILE_URL) + "/InterfaceImg/" + fileName;
					message.setResult(resultPath);
					System.out.println("resultPath--------------------" + resultPath);
				};
			} catch (IOException e) {
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage("图片转换失败");
				e.printStackTrace();
			} catch (FileSizeException e) {
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage("图片大小超出200KB");
				e.printStackTrace();
			} catch (ImageHeightWidthException e) {
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage("请上传宽为750px，高为1334px的图片");
				e.printStackTrace();
			}
		}
		System.out.println(message.getMessage());
		return message;
	}
	
	/**
	 * 界面管理邀请好友图片的上传
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/updoadInterfaceInvitationImg", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Object> updoadInterfaceInvitationImg(@RequestParam("file") MultipartFile file) {
		ResponseMessage<Object> message = new ResponseMessage<Object>();
		ImageModel imageModel = new ImageModel();
		//上传的图片全名字
		String oldFileName = file.getOriginalFilename();
		//上传的图片的后缀
		String extFileName = oldFileName.substring(oldFileName.lastIndexOf("."));
		//文件后缀判断 .jpg
		if(!extFileName.matches("^.*(jpg)$")){
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage("图片格式不正确,请上传jpg格式的图片");
		}else{
			//判断是否是木马文件
			try {
				if(file.getSize() > 200*1024){
					throw new FileSizeException("");
				}
				BufferedImage bufImage = ImageIO.read(file.getInputStream());
				if(bufImage.getWidth() != 750 || bufImage.getHeight() != 1334){
					throw new ImageHeightWidthException("");
				}
				imageModel.setWidth(bufImage.getWidth());
				imageModel.setHeight(bufImage.getHeight());
				String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/InterfaceImg/";
				
				String fileName = UserContext.getCurrentUserId() + "002" +extFileName;
				String result = httpClientUploadFile(file, path,  fileName);
				if(result.equals("\"success\"")){
					message.setCode(SysConstant.SUCCESS_CODE);
					message.setMessage(SysConstant.SUCCESS_MSG);
					String resultPath = PropUtis.getValue(PropUtis.FILE_URL) + "/InterfaceImg/" + fileName;
					message.setResult(resultPath);
					System.out.println("resultPath--------------------" + resultPath);
				};
			} catch (IOException e) {
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage("图片转换失败");
				e.printStackTrace();
			} catch (FileSizeException e) {
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage("图片大小超出200KB");
				e.printStackTrace();
			} catch (ImageHeightWidthException e) {
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage("请上传宽为750px，高为1334px的图片");
				e.printStackTrace();
			}
		}
		return message;
	}
	
	
	
	
	@RequestMapping(value = "/updoadVideo", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> updoadVideo(@RequestParam("file") MultipartFile file) {

		Long time = System.currentTimeMillis();

		java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

		String exname = FilenameUtils.getExtension(file.getOriginalFilename());
		String fileName =  f.format(new Date());
		String path = PropUtis.getValue(PropUtis.VIDEO_FILE_PATH) + "/" + fileName;
		System.out.println("path--------------------" + path);
		File filepath = new File(path);
		if (!filepath.exists()) {

			filepath.mkdirs();
		}

		ResponseMessage<String> message = new ResponseMessage<>();

		try {
			
			String result = httpClientUploadFile(file, path,  time + "." + exname);
			if(result.equals("\"success\"")){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				String resultPath = PropUtis.getValue(PropUtis.FILE_URL) + "/" + fileName + "/" + time + "."
						+ exname;
				message.setResult(resultPath);
				System.out.println("resultPath--------------------" + resultPath);
			};
			
		} catch (Exception e) {

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}
		return message;
	}
	
	@RequestMapping(value = "/uploads", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<String>> uploads(@RequestParam("file") List<MultipartFile> file) {


		ResponseMessage<List<String>> message = new ResponseMessage<>();

		try {
			
			List<String>  list= new ArrayList<>();
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			
			int i=0;
			
			for (MultipartFile multipartFile : file) {
				list.add(this.uploadFile(multipartFile,i));
				i++;
			}
			
			message.setResult(list);
			
		} catch (IOException e) {

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}

		return message;
	}
	@RequestMapping(value = "/uploadimageswithsize", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<ImageModel>> uploadImagesWithSize(@RequestParam("file") List<MultipartFile> file) {

		ResponseMessage<List<ImageModel>> message = new ResponseMessage<>();

		try {
			
			List<ImageModel>  list= new ArrayList<>();
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			
			int i=0;
			
			for (MultipartFile multipartFile : file) {
				ImageModel imageModel = new ImageModel();
				
				Long time = System.currentTimeMillis()+i;

				java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

				String exname = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
				String fileName =  f.format(new Date());
				String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/" + fileName;
				System.out.println("path--------------------" + path);
				File filepath = new File(path);
				if (!filepath.exists()) {

					filepath.mkdirs();
				}
					File write = new File(filepath.getPath() + "/" + time + "." + exname);
					System.out.println("file--------------------" + filepath.getPath() + "/" + time + "." + exname);
					write.createNewFile();
					FileUtils.writeByteArrayToFile(write, multipartFile.getBytes());
					
					String result = httpClientUploadFile(multipartFile, path,  time + "." + exname);
					if(result.equals("\"success\"")){
						String resultPath = PropUtis.getValue(PropUtis.FILE_URL) + "/" + fileName + "/" + time + "."
								+ exname;
						imageModel.setUrl(resultPath);
					};
				  
				
				BufferedImage sourceImg = ImageIO.read(write);
				imageModel.setHeight(sourceImg.getHeight());
				imageModel.setWidth(sourceImg.getWidth());
				list.add(imageModel);
				i++;
			}
			
			message.setResult(list);
			
		} catch (IOException e) {

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}

		return message;
	}
	private String uploadFile(MultipartFile file,int i) throws IOException{
		
		
		Long time = System.currentTimeMillis()+i;

		java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

		String exname = FilenameUtils.getExtension(file.getOriginalFilename());
		String fileName =  f.format(new Date());
		String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/" +fileName;
		

		String result = httpClientUploadFile(file, path,  time + "." + exname);
		String resultPath ="";
		if(result.equals("\"success\"")){
			resultPath = PropUtis.getValue(PropUtis.FILE_URL) + "/" + fileName + "/" + time + "."
					+ exname;
		};
		  return resultPath;
	}

	@RequestMapping(value = "/updoadimgbase64", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> updoadimgbase64(@RequestBody HashMap<String, String> map) {

		ResponseMessage<String> message = new ResponseMessage<>();
		String savePath = "";
		try {
			String imgBase64 = map.get("imgBase64");
			if (!StringUtils.isEmpty(imgBase64)) {
				savePath = saveBase64Img(imgBase64);
				if(null == savePath || "".equals(savePath)){
					message.setCode(SysConstant.FAILURE_CODE);
					message.setMessage(SysConstant.FAILURE_MSG);
				}else{
					message.setCode(SysConstant.SUCCESS_CODE);
					message.setMessage(SysConstant.SUCCESS_MSG);
				}
					message.setResult(savePath);
			}
			

		} catch (Exception e) {

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}
		return message;
	}

	@RequestMapping(value = "/updoadimgbase64ForImgList", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<String>> updoadimgbase64ForImgList(@RequestBody List<String> list) {

		ResponseMessage<List<String>> message = new ResponseMessage<>();
		message.setCode(SysConstant.SUCCESS_CODE);
		message.setMessage(SysConstant.SUCCESS_MSG);
		List<String> savePath = new ArrayList<String>();
		try {
			for (String s : list) {
				if (!StringUtils.isEmpty(s)) {
					String path = saveBase64Img(s);
					if(null != path && ! "".equals(savePath)){
						savePath.add(path);
					}
				}
			}
			message.setResult(savePath);
		} catch (Exception e) {
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}
		return message;
	}
	@RequestMapping(value = "/updoadForMap", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Map<String, String>> updoadForImgList(@RequestBody Map<String, String> map ) {

		ResponseMessage<Map<String, String>> message = new ResponseMessage<>();
		message.setCode(SysConstant.SUCCESS_CODE);
		message.setMessage(SysConstant.SUCCESS_MSG);
		for (Iterator<String> keys = map.keySet().iterator();keys.hasNext(); ) {
			String key = keys.next();
		try {
				if (!StringUtils.isEmpty(map.get(key))) {
					String path = saveBase64Img(map.get(key));
					if(null != path && ! "".equals(path)){
						map.put(key, path);
					}
					
				}
		} catch (Exception e) {
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}
		}
		message.setResult(map);
		return message;
	}
	
	public static void main(String[] args) {

		Map<String, String> m = new HashMap<String, String>();
		m.put("imgBase64", "http:////dfdfdf.jpg");
		System.out.println(new Gson().toJson(m));

	}

	@RequestMapping(value="/server/uploadCaseImage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> uploadCaseImage(@RequestParam("file") MultipartFile file){
		
		String name = SysConstant.CASE_IMAGE_NAME+UserContext.getCurrentUserId();
		
		String exname=FilenameUtils.getExtension(file.getOriginalFilename());
		
		String  path=PropUtis.getValue(PropUtis.FILE_PATH)+"/caseimage";
		
		
		
		
		ResponseMessage<String> message = new ResponseMessage<>();
		
		try {
			String result = httpClientUploadFile(file, path,  name + "." + exname);
			if(result.equals("\"success\"")){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				String resultPath = PropUtis.getValue(PropUtis.FILE_URL) + "/caseimage"  + "/" + name + "."
						+ exname;
				RedisUtils.setValue(name, resultPath);
				message.setResult(resultPath);
				System.out.println("resultPath--------------------" + resultPath);
			};
			
		} catch (Exception e) {
			
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}
		
		return message;
	}
	
	
	@RequestMapping(value = "/server/uploadshopbg", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> uploadbrandbg(@RequestParam("file") MultipartFile file) {

		String name = SysConstant.SHOP_BG_NAME+UserContext.getCurrentUserId();

		String exname = FilenameUtils.getExtension(file.getOriginalFilename());

		String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/image";		

		ResponseMessage<String> message = new ResponseMessage<>();

		try {

			
			String result = httpClientUploadFile(file, path,  name + "." + exname);
			if(result.equals("\"success\"")){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				String resultPath =PropUtis.getValue(PropUtis.FILE_URL)+"/image/"+name+"."+exname;
				RedisUtils.setValue(name, resultPath);
				message.setResult(resultPath);
				System.out.println("resultPath--------------------" + resultPath);
			};

		} catch (Exception e) {

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}

		return message;
	}
	
	@RequestMapping(value="/server/updloadpointmallimg",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> uploadPointMallImg(@RequestParam("file") MultipartFile file){
		
		String exname=FilenameUtils.getExtension(file.getOriginalFilename());
		
		String  path=PropUtis.getValue(PropUtis.FILE_PATH)+"/image";
		
		ResponseMessage<String> message = new ResponseMessage<>();
		
		try {
			
			
			String result = httpClientUploadFile(file, path,  SysConstant.POINTMALL_IMAGE_NAME + "." + exname);
			if(result.equals("\"success\"")){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				String resultPath =PropUtis.getValue(PropUtis.FILE_URL)+"/image/"+ SysConstant.POINTMALL_IMAGE_NAME+"."+exname;
				RedisUtils.setValue(SysConstant.POINTMALL_IMAGE_NAME, resultPath);
				message.setResult(resultPath);
				System.out.println("resultPath--------------------" + resultPath);
			};
			
		} catch (Exception e) {
			
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}
		
		return message;
	}
	
	
	/**
	 * {"state": "SUCCESS","original": "image_6_401.jpg","size": "65818","title": "1474872848492079398.jpg","type": ".jpg","url": "/file/upload/20160926/1474872848492079398.jpg"}
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/ueupload", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Map<String,Object> ueUpload(@RequestParam("file") MultipartFile file) {

		Long time = System.currentTimeMillis();

		Map<String,Object> responseMap = new HashMap<>();
		
		
		java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		String filName =  f.format(new Date());
		String exname = FilenameUtils.getExtension(file.getOriginalFilename());

		String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/" +filName;

		

		ResponseMessage<String> message = new ResponseMessage<>();

		try {
			String result = httpClientUploadFile(file, path,  time+ "." + exname);
			
			
			responseMap.put("state", "SUCCESS");
			responseMap.put("original", file.getOriginalFilename());
			responseMap.put("size", file.getSize());
			responseMap.put("title", time+"."+exname);
			responseMap.put("type", "."+exname);
		
			
			if(result.equals("\"success\"")){
				String resultPath = PropUtis.getValue(PropUtis.FILE_URL) + "/" + filName+ "/" + time + "."
						+ exname;
				responseMap.put("url", resultPath);
			};
			
		} catch (Exception e) {

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}

		return responseMap;
	}

	public static String httpClientUploadFile(MultipartFile file, String filePath, String fileName) {
        final String remote_url = "http://www.kaolaj.com/shop-web/common/httpClientUpload";// 第三方服务器请求地址
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result = "";
        try {
            HttpPost httpPost = new HttpPost(remote_url);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
            builder.addTextBody("filePath", filePath);// 类似浏览器表单提交，对应input的name和value
            builder.addTextBody("fileName", fileName);
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);// 执行提交
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                // 将响应内容转换为字符串
                result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static String httpClientUploadFileBase64(byte[] bytes, String filePath, String fileName) {
        final String remote_url = "http://www.kaolaj.com/shop-web/common/httpClientUpload";// 第三方服务器请求地址
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result = "";
        try {
            HttpPost httpPost = new HttpPost(remote_url);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addBinaryBody("file", bytes, ContentType.APPLICATION_FORM_URLENCODED, "");// 文件流
            builder.addTextBody("filePath", filePath);// 类似浏览器表单提交，对应input的name和value
            builder.addTextBody("fileName", fileName);// 类似浏览器表单提交，对应input的name和value
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);// 执行提交
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                // 将响应内容转换为字符串
                result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    public static String cutImage(String srcImageFile,String result,int destWidth,int destHeight) {
		String responseContent =HttpRequest.sendPost("http://www.kaolaj.com/shop-web/common/cutImage", "srcImageFile="+srcImageFile+"&result="+result+"&destWidth="+destWidth+"&destHeight="+destHeight);
		Map<String, Object> map = JSON.parseObject(responseContent);
		return map.get("message").toString();
    }

    public static String saveBase64Img(String imgBase64) throws IOException {
        String s = DateUtils.formatDateYmd(new Date());
        String savePath = PropUtis.getValue(PropUtis.FILE_PATH) + "/" + s;
        System.out.println("savePath=-----------------------" + savePath);
        String filePath = "";
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] imgByte = decoder.decodeBuffer(imgBase64.substring(imgBase64.indexOf(",") + 1));
        for (int i = 0; i < imgByte.length; ++i) {
            if (imgByte[i] < 0) {// 调整异常数据
                imgByte[i] += 256;
            }
        }
        Random random = new Random();
        filePath = DateUtils.formatDateNospace(new Date()) + "-" + random.nextInt(10) + random.nextInt(10)
                + random.nextInt(10) + ".png";
        String fileBase64 = httpClientUploadFileBase64(imgByte, savePath, filePath);
        if("\"success\"".equals(fileBase64)){
            System.out.println("rturnPath=-----------------------" + PropUtis.getValue(PropUtis.FILE_URL) + "/" + s + "/"+filePath);
            return PropUtis.getValue(PropUtis.FILE_URL) + "/" + DateUtils.formatDateYmd(new Date()) + filePath;
        }else{
            return null;
        }
    }

    

}
